package apptoko.com.view;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import apptoko.com.R;
;

public class MyWebView extends Activity {
	String url;
	Intent callbackIntent;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mywebview);
		callbackIntent=getIntent();
		Bundle extras = callbackIntent.getExtras();
		if (extras != null) {
			url = extras.getString("url");
		}
		WebView webview = (WebView) findViewById(R.id.myWeb);
		webview.requestFocus(View.FOCUS_DOWN);
		webview.setWebViewClient( new WebViewClient()
        {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                if( url.contains("apptokotwitter?oauth_token="))
                {
                    Uri uri = Uri.parse( url );
                    String oauthVerifier = uri.getQueryParameter( "oauth_verifier" );
                    callbackIntent.putExtra( "oauth_verifier", oauthVerifier );
                    setResult( RESULT_OK, callbackIntent );
                    finish();
                    return true;
                }else if(url.contains("apptokotwitter?denied=")){
                	setResult(RESULT_CANCELED);
                	finish();
                    return true;
                }
                return false;
            }
        });
		webview.loadUrl(url);
    }

	}

//	private void goBack(String url) {
//		final Uri uri = Uri.parse(url);
//		if (uri != null
//				&& uri.getScheme().equals(LoginActivity.URL_TWITTER_AUTH)) {
//			// new
////			 RetrieveAccessTokenTask(this,MyWebView.getConsumer(),PrepareRequestTokenActivity.getProvider(),prefs).execute(uri);
//			try{
//				Twitter twitter;
//				String verifier = uri
//	                    .getQueryParameter(LoginActivity.URL_TWITTER_OAUTH_VERIFIER);
//				///
//				ConfigurationBuilder builder = new ConfigurationBuilder();
//	            builder.setOAuthConsumerKey(Constants.TWITTER_CONSUMER_KEY);
//	            builder.setOAuthConsumerSecret(Constants.TWITTER_CONSUMER_SECRET);
//	            Configuration configuration = builder.build();
//	            
//	            TwitterFactory factory = new TwitterFactory(configuration);
//	            twitter = factory.getInstance();
//	            RequestToken requestToken = null;
//	                requestToken = twitter
//	                        .getOAuthRequestToken(LoginActivity.TWITTER_CALLBACK_URL);
//	                this.startActivity(new Intent(Intent.ACTION_VIEW, Uri
//	                        .parse(requestToken.getAuthenticationURL())));
//				///
//				twitter4j.auth.AccessToken accessToken = twitter.getOAuthAccessToken(requestToken, verifier);
//				Log.e("tVerifier",verifier+", TaccessToken:"+accessToken.getToken());
////	            Editor e = mSharedPreferences.edit();
////	            e.putString(PREF_KEY_OAUTH_TOKEN, accessToken.getToken());
////	            e.putString(PREF_KEY_OAUTH_SECRET,
////	                    accessToken.getTokenSecret());
////	            e.putBoolean(PREF_KEY_TWITTER_LOGIN, true);
////	            e.commit();
////			finish();
//		}catch(Exception ex){
//			ex.printStackTrace();
//		}
//		}
//
//	}
//
//	public class RetrieveAccessTokenTask extends AsyncTask<Uri, Void, Void> {
//
//		private Context	context;
//		private OAuthProvider provider;
//		private OAuthConsumer consumer;
//
//		public RetrieveAccessTokenTask(Context context, OAuthConsumer consumer,OAuthProvider provider) {
//		this.context = context;
//		this.consumer = consumer;
//		this.provider = provider;
//		}
//
//		/**
//		* Retrieve the oauth_verifier, and store the oauth and oauth_token_secret
//		* for future API calls.
//		*/
//		@Override
//		protected Void doInBackground(Uri...params) {
//		final Uri uri = params[0];
//		final String oauth_verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
//
//		try {
//		provider.retrieveAccessToken(consumer, oauth_verifier);
////		
////		final Editor edit = prefs.edit();
////		edit.putString(OAuth.OAUTH_TOKEN, consumer.getToken());
////		edit.putString(OAuth.OAUTH_TOKEN_SECRET, consumer.getTokenSecret());
////		edit.commit();
////
////		String token = prefs.getString(OAuth.OAUTH_TOKEN, "");
////		String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, "");
//
////		consumer.setTokenWithSecret(token, secret);
////		context.startActivity(new Intent(context,AndroidTwitterActivity.class));
//
//		executeAfterAccessTokenRetrieval();
//
//		} catch (Exception e) {
//		e.printStackTrace();
//		}
//
//		return null;
//		}
//
//		private void executeAfterAccessTokenRetrieval() {
//		String msg = getIntent().getExtras().getString("tweet_msg");
//		try {
////			 TwitterUtils.sendTweet(prefs, "This is a tweet");
//		} catch (Exception e) {
//		e.printStackTrace();
//		}
//		}
//	}
//	public class Constants {
//
//		public static final String CONSUMER_KEY = "<FILL IN YOUR CONSUMER KEY FROM TWITTER HERE>";
//		public static final String CONSUMER_SECRET= "<FILL IN YOUR CONSUMER SECRET FROM TWITTER HERE>";
//
//		public static final String REQUEST_URL = "http://api.twitter.com/oauth/request_token";
//		public static final String ACCESS_URL = "http://api.twitter.com/oauth/access_token";
//		public static final String AUTHORIZE_URL = "http://api.twitter.com/oauth/authorize";
//
//		final public static String	CALLBACK_SCHEME = "x-latify-oauth-twitter";
//		final public static String	CALLBACK_URL = CALLBACK_SCHEME + "://callback";
//
//		}
